package com.ruoyi.system.api;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author: cjie
 * @description: 操作关联附件
 * @date: 2021/7/21
 */

@Service
public class LinkFileServiceImpl{
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 业务关联附件
     * @param fileIdList 附件id
     * @param id 业务id
     */
    public void linkFile(List<Long> fileIdList,String id) {
        if(fileIdList != null){
            fileIdList.forEach(fileId->{
                R<SysFile> one = remoteFileService.getOne(fileId);
                if(one.getCode()== Constants.SUCCESS){
                    SysFile sysFile = one.getData();
                    sysFile.setLinkId(id);
                    remoteFileService.update(sysFile);
                }

            });
        }
    }

    /**
     * 删除关联附件
     *
     * @param ids
     */
    public void deleteLinkFile(String[] ids) {
        Arrays.stream(ids).forEach(remoteFileService::deleteLinkFile);
    }

    /**
     * 通过linkId获取关联文件
     *
     * @param linkId
     * @return
     */
    public List<SysFile> getFileList(String linkId) {
        R<List<SysFile>> r = remoteFileService.getList(linkId);
        if(r.getCode() == Constants.SUCCESS){
            return r.getData();
        }else{
            return null;
        }
    }
}
